/**
 * Simulator
 */
var Simulator = function() {
	var buttonElems = {
		polyline: "#toolbar-polyline",
		polygon: "#toolbar-polygon",
		rectangle: "#toolbar-rectangle",
		spawn: "#toolbar-spawn",
		endpoint: "#toolbar-endpoints",
		route: "#toolbar-route",
		geocode: "#geocode-btn",
		toolbar: "#toolbar-radios"
	};

	/**
	 * Build jquery ui buttons
	 */
	function buttons() {
		$(buttonElems.polyline).button({ icons: { primary: "ui-icon-pencil" } });
		$(buttonElems.polygon).button({ icons: { primary: "ui-icon-disk" } });
		$(buttonElems.rectangle).button({ icons: { primary: "ui-icon-gear" } });

		$(buttonElems.spawn).button({
			text: false,
			icons: { primary: "ui-icon-person" }
		});

		$(buttonElems.endpoint).button({
			text: false,
			icons: { primary: "ui-icon-flag" }
		});

		$(buttonElems.route).button({
			text: false,
			icons: { primary: "ui-icon-flag" }
		});
		// Geocode search button
		$(buttonElems.geocode).button({ icons: { primary: "ui-icon-search"} });
		// Radio buttons
		$(buttonElems.toolbar).buttonset();
	}

	function initialize() {
		var myMap = Map();
		var map = myMap.init();
		var simData = SimData(map);
		buttons();

		/**
		 * Draw polyline
		 */
		$(buttonElems.polyline).click(function() {
			google.maps.event.clearListeners(map);
			simData.drawPolyline();
		});

		/**
		 * Draw polygon
		 */
		$(buttonElems.polygon).click(function() {
			google.maps.event.clearListeners(map);
			simData.drawPolygon();
		});

		/**
		 * Generate street route
		 */
		$(buttonElems.route).click(function() {
			simData.sendOverlays();
			//console.log(simData.getPolygons()[0][0].lat());
			//console.log(simData.getPolygons()[0][0].lng());
			/*
			if (spawnPoints.length != 0 && endPoints.length != 0) {
				var position = {
					origin: spawnPoints.toString(),
					dest: endPoints.toString()
				};
				simData.directions(position);
			}
			*/
		});

		/**
		 * Add Spawn point
		 */
		$(buttonElems.spawn).click(function() {
			google.maps.event.clearListeners(map);
			google.maps.event.addListener(map, 'click', function(e) {
				var options = {
					position: e.latLng,
					color: "00FF00",
					text: "Start",
					type: "start"
				};
				simData.placeMarker(options);
			});
		});

		/**
		 * Place end point
		 */
		$(buttonElems.endpoint).click(function() {
			google.maps.event.clearListeners(map);
			google.maps.event.addListener(map, 'click', function(e) {
				var options = {
					position: e.latLng,
					color: "FF0000",
					text: "End",
					type: "end"
				};
				simData.placeMarker(options);
			});
		});
	}

	/**
	 * Public methods
	 */
	return {
		init: function() {
			initialize();
		}
	};
};

/**
 * Execute on window ready
 */
$(function() {
	simulator = Simulator();
	simulator.init();
});
